package com.oco.restcontroller.api.resttemplate.userinfo.web;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.oco.restcontroller.api.resttemplate.common.Constants;
import com.oco.restcontroller.api.resttemplate.common.RESTRibbon;
import com.oco.restcontroller.api.resttemplate.common.ResultMap;
import com.oco.restcontroller.api.resttemplate.userinfo.param.UserInfoVO;

/**
 * 用户信息RESTFul接口
 * @author zhangzhihong
 * @email it_javasun@yeah.net
 * @since 2016年8月10日上午9:39:56
 */
@RestController
@RequestMapping(value="${spring.web.apiversion}/userinfo")
public class RestUserIfoController {

	private static final Logger logger = LoggerFactory.getLogger(RestUserIfoController.class);
	
	@Resource
	private RESTRibbon restRibbon;
	
	/**
	 * 测试接口
	 * @author zhangzhihong
	 * @email it_javasun@yeah.net
	 * @since 2016年8月10日下午6:34:12
	 */
	@RequestMapping(value="/getTest", method = RequestMethod.POST, headers = "Accept=application/json")
	public @ResponseBody Map<String, Object> getTest(@RequestBody UserInfoVO userInfoVO) throws JsonProcessingException{
		String returnIds = ResultMap.objectMapper().writeValueAsString(userInfoVO);
		if (logger.isDebugEnabled()) {
			logger.debug("RESTFul测试数据");
		}
		return new ResultMap().success().data(returnIds).map();
	}
	
	/**
	 * 测试调用远程费服务
	 * 		调用远程etcd上注册的服务
	 *      请求的参数必须与服务接口参数保持一致：RequestMethod.GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
	 *      	
	 * @author zhangzhihong
	 * @email it_javasun@yeah.net
	 * @since 2016年8月10日下午6:34:12
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/getRemoteService", method = RequestMethod.POST, headers = "Accept=application/json")
	public @ResponseBody Map<String, Object> getRemoteService(@RequestBody Map<String, Object> paramMap) throws JsonProcessingException{
		
		String json = ResultMap.objectMapper().writeValueAsString(paramMap);
		String url = restRibbon.EXCHANGEURI() + Constants.REDEEM_SEARCH;
		ResponseEntity<Map> response = restRibbon.restTemplate().exchange(url, HttpMethod.POST,
				formEntity(json), Map.class);
		HttpStatus status = response.getStatusCode();
		if (status == HttpStatus.OK && response.hasBody()) {
			Map<String, Object> partnerMap = response.getBody();
			if (partnerMap != null && !partnerMap.isEmpty()) {
				Object data = partnerMap.get("data");
				return new ResultMap().success().data(data).map();
				} else {
					return new ResultMap().failure().errmsg("未查询到兑换码信息！").map();
				}
		} else {
			return new ResultMap().failure().errmsg("调用查询到兑换码信息接口失败！").map();
		}
	}
	
	
	
	private HttpEntity<String> formEntity(String json) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
		return formEntity;
	}
}
